import _ from 'lodash';

const hr = {
  state: {
    dynamicOrg: []
  },
  mutations: {
    getDynamicOrg (state, dynamicOrg) {
      state.dynamicOrg = dynamicOrg;
    }
  },
  actions: {
    saveDynamicOrg () {
      return new Promise((resolve, reject) => {
        this.$http.get('/hr/orglist')
          .then(function (res) {
            dynamicOrg = _.map(res.data, o => {
              o.value = o.org_id
              o.label = o.org_name
              o.loading = false
              o.children = []
              return o
            })
            commit('getDynamicOrg', dynamicOrg);
            resolve();
          }).catch(function (error) {
            reject(error);
          });
      });
    },
    saveLoadOrgs() {
      return new Promise((resolve, reject) => {
        this.$http.get('/hr/orglist')
          .then(function (res) {
            dynamicOrg = _.map(res.data, o => {
              o.value = o.org_id
              o.label = o.org_name
              o.loading = false
              o.children = []
              return o
            })
            commit('getDynamicOrg', dynamicOrg);
            resolve();
          }).catch(function (error) {
            reject(error);
          });
      });
    }
  },
  // getters: {
  //   selected: state => {
  //     return state.orgList.filter(org => org.selected);
  //   }
  // }
};

export default hr;
